<template>
  <div>
    <Modal :value="showModal" :title="handleType" @on-visible-change="changeVisible">
      <Form ref="form" :model="formData" :rules="ruleCustom" :label-width="80">
        <FormItem style="display: none" v-if="formData.id">
          <label>
            <Input v-model="formData.id" :value="formData.id"/>
          </label>
        </FormItem>
        <FormItem label="部门名" prop="name">
          <label>
            <Input v-model="formData.name" clearable placeholder="请填写部门名称"/>
          </label>
        </FormItem>
        <FormItem label="管理人" prop="owner">
          <label>
            <Select clearable transfer filterable clearable v-model="formData.owner">
              <Option v-for="item in usersData" :value="item.user_id" :key="item.user_id">{{ item.name }}</Option>
            </Select>
          </label>
        </FormItem>
        <FormItem label="审核人" prop="master">
          <label>
            <Select clearable transfer filterable clearable v-model="formData.master">
              <Option v-for="item in usersData" :value="item.user_id" :key="item.user_id">{{ item.name }}</Option>
            </Select>
          </label>
        </FormItem>
        <Button
          long
          type="primary"
          size="large"
          @click="handleSubmit('form')">确认
        </Button>
      </Form>
      <div slot="footer"/>
    </Modal>
  </div>
</template>

<script>

  export default {
    name: 'AddWorkspace',
    props: {
      showModal: {
        type: Boolean,
        default: false
      },
      handleType: {
        type: String,
        default: '编辑部门'
      },
      usersData: {
        type: Array,
        default () {
          return []
        }
      },
      formData: {
        type: Object,
        default () {
          return {}
        }
      }
    },
    mounted () {
    },
    data () {
      return {
        loading: true,
        ruleCustom: {
          name: [
            { required: true, message: '名称必填', trigger: 'blur' }
          ],
          owner: [
            { required: true, message: '审核人必填', trigger: 'blur', type: 'number' },
          ],
          master: [
            { required: true, message: '管理人必填', trigger: 'blur', type: 'number' },
          ],
        }
      }
    },
    methods: {
      handleSubmit (name) {
        this.$refs[name].validate((valid) => {
          if (valid) {
            this.$emit('handleSubmit', this.formData)
          } else {
            return false
          }
        })
      },
      changeVisible: function (val) {
        this.$refs['form'].resetFields()
        this.$emit('changeVisible', val)
      },
    }
  }
</script>

<style scoped>

</style>
